{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "#定义和基本属性\n",
    "array=np.array([[1,2,3],[4,5,6]])\n",
    "print(array)\n",
    "#维度数，形状，元素个数\n",
    "print('number of dim:',array.ndim)\n",
    "print('shape:',array.shape)\n",
    "print('size:',array.size)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "#定义元素类型 ,dtype=   \n",
    "array=np.array([[1,2,3],[4,5,6]],dtype=np.int64)\n",
    "print(array.dtype)\n",
    "\n",
    "#全零矩阵\n",
    "a=np.zeros((2,3),dtype=np.int64)\n",
    "print(a)\n",
    "\n",
    "#全1矩阵 \n",
    "a=np.ones((3,4),dtype=np.int64)\n",
    "print(a)    \n",
    "\n",
    "#empty矩阵 趋近于零\n",
    "a=np.empty((3,4))\n",
    "print(a)   \n",
    "\n",
    "#有序矩阵 (起始值 最终值 步长),-1可以自动计算维度\n",
    "a=np.arange(1,10,1).reshape((1,9))\n",
    "print(a)\n",
    "\n",
    "#linspace (起始值 最终值 段数 )\n",
    "a=np.linspace(1,10,20).reshape(4,5)\n",
    "print(a)\n",
    "\n",
    "#生成0-1的矩阵 \n",
    "a=np.random.random((3,4))\n",
    "print(a)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n",
      "[12 15 18 21]\n",
      "[ 3  7 11]\n",
      "[0 4 8]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "a=np.array([10,20,30,40])\n",
    "b=np.arange(4)\n",
    "\n",
    "\n",
    "#基础运算 +,-,**(乘方不是^)\n",
    "c=a+b\n",
    "print(c)\n",
    "c=a-b\n",
    "print(c)\n",
    "c=b**2\n",
    "print(c)\n",
    "\n",
    "#三角函数 sin  cos  tan\n",
    "c=np.sin(a)\n",
    "print(c)\n",
    "c=np.cos(a)\n",
    "print(c)\n",
    "c=np.tan(a)\n",
    "print(c)\n",
    "\n",
    "#对元素判断返回布尔矩阵\n",
    "print(b>2)\n",
    "\n",
    "a=np.array([[1,1],[0,1]])\n",
    "b=np.arange(4).reshape(2,-1)\n",
    "\n",
    "#元素乘法*  矩阵乘法dot()\n",
    "c=a*b\n",
    "print(c)\n",
    "c=np.dot(a,b)\n",
    "print(c)\n",
    "c=b.dot(a)\n",
    "print(c)\n",
    "\n",
    "#求和 min max axis=,0代表沿着列，1代表沿着行\n",
    "a=np.arange(12).reshape(3,4)\n",
    "print(a)\n",
    "print(np.sum(a,axis=0))\n",
    "print(np.max(a,axis=1))\n",
    "print(np.min(a,axis=1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "A=np.arange(2,14).reshape((3,4))\n",
    "print(A)\n",
    "# [[ 2  3  4  5]\n",
    "#  [ 6  7  8  9]\n",
    "#  [10 11 12 13]]\n",
    "\n",
    "#最大值索引，最小值索引，均值，中位数 \n",
    "print(np.argmax(A))\n",
    "print(np.argmin(A))\n",
    "print(np.mean(A,axis=0))\n",
    "print(np.median(A,axis=1))\n",
    "\n",
    "#前缀和  [ 2  5  9 14 20 27 35 44 54 65 77 90]\n",
    "print(np.cumsum(A))\n",
    "\n",
    "#差分  \n",
    "print(np.diff(A))\n",
    "# [[1 1 1]\n",
    "#  [1 1 1]\n",
    "#  [1 1 1]]\n",
    "\n",
    "#非零元素索引\n",
    "print(np.nonzero(A))\n",
    "\n",
    "#sort排序,逐行从小到大排序 \n",
    "A=np.arange(14,2,-1).reshape((3,-1))\n",
    "print(A)\n",
    "print(np.sort(A))\n",
    "\n",
    "#矩阵转置\n",
    "print(np.transpose(A))\n",
    "print(A.T)\n",
    "\n",
    "#clip操作,小于5的值变为5，大于9的值变为9\n",
    "print(np.clip(A,5,9))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "A=np.arange(3,15).reshape((3,4))\n",
    "print(A)\n",
    "#numpy的索引从零开始 \n",
    "#第零行\n",
    "print(A[0])\n",
    "#第二行第一列\n",
    "print(A[2][1])\n",
    "print(A[2,1])\n",
    "#：冒号代表所有\n",
    "print(A[2,:])\n",
    "print(A[2,0:1])#第二行0-1列 \n",
    "print(A[0:2,0:2])#0-2行的0-2列 \n",
    "\n",
    "\n",
    "#for循环迭代行\n",
    "for row in A:\n",
    "    print(row)\n",
    "\n",
    "#for循环迭代列，借助转置\n",
    "for col in A.T:\n",
    "    print(col)\n",
    "\n",
    "#迭代元素,flat生成迭代器，flatten()展平\n",
    "print(A.flatten())\n",
    "for item in A.flat:\n",
    "    print(item)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "A=np.array([1,1,1])\n",
    "B=np.array([2,2,2])\n",
    "\n",
    "#上下合并\n",
    "C=np.vstack((A,B))\n",
    "print(C)\n",
    "print(C.shape)\n",
    "#左右合并\n",
    "D=np.hstack((A,B))\n",
    "print(D)\n",
    "print(D.shape)\n",
    "\n",
    "#添加维度np.newaxis\n",
    "print(A[:,np.newaxis].shape)\n",
    "A=np.array([1,1,1])[:,np.newaxis]\n",
    "B=np.array([2,2,2])[:,np.newaxis]\n",
    "C=np.vstack((A,B))\n",
    "print(C)\n",
    "D=np.hstack((A,B))\n",
    "print(D)\n",
    "print(D.shape)\n",
    "\n",
    "#多个array合并,axis=0代表上下合并，即行的维度 \n",
    "C=np.concatenate((A,B,B,A),axis=0)\n",
    "print(C)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "\n",
    "A=np.arange(3,15).reshape((3,4))\n",
    "#numpy分割 split(A,段数,维度)\n",
    "\n",
    "#axis=0,沿着行向均匀分割\n",
    "print(np.split(A,3,axis=0))\n",
    "\n",
    "#axis=1,沿着列向均匀分割\n",
    "print(np.split(A,2,axis=1))\n",
    "\n",
    "#axis=1,沿着列向不均匀分割\n",
    "print(np.array_split(A,3,axis=1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "#浅拷贝,a的值改变后，b也跟着改变\n",
    "a=np.arange(4)\n",
    "b=a\n",
    "a[0]=10\n",
    "print(a)\n",
    "print(b)\n",
    "print(b is a)\n",
    "\n",
    "#深拷贝,a的值改变后，b不会跟着改变，开辟了新的内存地址\n",
    "a=np.arange(6)\n",
    "b=a.copy()#\n",
    "a[0]=100\n",
    "print(a)\n",
    "print(b)\n",
    "print(b is a)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "pytorch",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
